package com.victualler.business.purchase.service.impl;

import com.victualler.business.purchase.dto.PrePurchaseOrderInfoDto;
import com.victualler.business.purchase.entity.PrePurchaseItemDao;
import com.victualler.business.purchase.entity.PrePurchaseOrderDao;
import com.victualler.business.purchase.enums.CheckStateEnum;
import com.victualler.business.purchase.repository.PrePurchaseOrderQueryRepository;
import com.victualler.business.purchase.repository.PrePurchaseOrderRepository;
import com.victualler.business.purchase.service.PrePurchaseItemService;
import com.victualler.business.purchase.service.PrePurchaseOrderService;
import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.common.enums.ChecKDataErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class PrePurchaseOrderServiceImpl extends BaseServiceImpl<PrePurchaseOrderDao,PrePurchaseOrderRepository,PrePurchaseOrderQueryRepository> implements PrePurchaseOrderService {

    @Autowired
    private PrePurchaseItemService prePurchaseItemService;

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    public PrePurchaseOrderInfoDto findInfo(Long id) {
        PrePurchaseOrderDao prePurchaseOrderDao = this.getById(id);
        List<PrePurchaseItemDao> prePurchaseItemDaoList = this.prePurchaseItemService.getByParentId(id);
        return new PrePurchaseOrderInfoDto(prePurchaseOrderDao,prePurchaseItemDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param prePurchaseOrderInfoDto 预订单实体大对象
     * @Function  保存预订单实体大对象
     */
    @Transactional
    public void saveInfo(PrePurchaseOrderInfoDto prePurchaseOrderInfoDto) {
        //如果是提交类型 则更新提交时间
        if (CheckStateEnum.UN_CHECK.getKey().equals(String.valueOf(prePurchaseOrderInfoDto.getPrePurchaseOrderDao().getCheckState()))) {
            prePurchaseOrderInfoDto.getPrePurchaseOrderDao().setCommitDate(new Date());
        }
        if(prePurchaseOrderInfoDto.getPrePurchaseOrderDao().getId() == null) {
            //生成订单号
            this.save(prePurchaseOrderInfoDto.getPrePurchaseOrderDao());
            prePurchaseOrderInfoDto.getPrePurchaseOrderDao().setOrderNo("P" + prePurchaseOrderInfoDto.getPrePurchaseOrderDao().getId());
            //刷新字表数据
            for (PrePurchaseItemDao prePurchaseItemDao : prePurchaseOrderInfoDto.getPrePurchaseItemList()) {
                prePurchaseItemDao.setParentId(prePurchaseOrderInfoDto.getPrePurchaseOrderDao().getId());
            }
            this.prePurchaseItemService.saveAll(prePurchaseOrderInfoDto.getPrePurchaseItemList());
        } else {
            //更新
            this.save(prePurchaseOrderInfoDto.getPrePurchaseOrderDao());
            //更新明细信息
            this.prePurchaseItemService.updateList(prePurchaseOrderInfoDto.getPrePurchaseItemList(),prePurchaseOrderInfoDto.getPrePurchaseOrderDao().getId());
        }
    }

    /**
     * @author Guoshichang
     * @since 18:01 on 2018/10/9.
     * @param prePurchaseOrderDao 审查大对象
     * @return
     * @Function
     */
    @Transactional
    public void check(PrePurchaseOrderDao prePurchaseOrderDao) {
        PrePurchaseOrderDao saveObject = this.getById(prePurchaseOrderDao.getId());
        saveObject.setCheckDate(new Date());
        saveObject.setCheckUser(prePurchaseOrderDao.getCheckUser());
        saveObject.setCheckUserCode(prePurchaseOrderDao.getCheckUserCode());
        saveObject.setCheckRemark(prePurchaseOrderDao.getCheckRemark());
        saveObject.setCheckState(prePurchaseOrderDao.getCheckState());
        this.save(saveObject);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  删除草稿订单
     */
    @Transactional
    public void deleteInfo(Long id) {
        PrePurchaseOrderDao prePuchaseOrderDao = this.getById(id);
        //只有草稿数据允许删除
        if(CheckStateEnum.TEMP.getKey().equals(String.valueOf(prePuchaseOrderDao.getCheckState()))) {
            //删除所有信息
            this.deleteById(id);
            this.prePurchaseItemService.deleteByParentId(id);
        } else {
            String stateName =  CheckStateEnum.getValue(String.valueOf(prePuchaseOrderDao.getCheckState()));
            throw ChecKDataErrorEnum.createException(ChecKDataErrorEnum.DATA_ERROR,stateName + "状态不可删除!");
        }
    }
}
